/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_DB_RDB_FIELD_H_
#define NODE_DB_RDB_FIELD_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <string>

#include "node/macros.h"

namespace node {
    namespace rdb {

        class Field
        {
        public:
            enum DataTypes
            {
                TYPE_UNKNOWN = 0x00,
                TYPE_STRING = 0x01,
                TYPE_INTEGER = 0x02,
                TYPE_FLOAT = 0x03,
                TYPE_BOOL = 0x04
            };

        public:
            Field(void);
            Field(const char* value, enum DataTypes type);
            ~Field(void);

        public:
            inline void set_type(enum DataTypes type) { type_ = type; }
            inline enum DataTypes get_type(void) const { return type_; }
            inline void set_value(const char* value) { value_ = value; }
            inline bool IsNull(void) const {
                return value_ == NULL;
            }
            inline const char* GetCString(void) const {
                return value_;
            }
            inline ::std::string GetString(void) const {
                return ::std::string(((value_ != NULL) ? value_ : ""));
            }
            inline float GetFloat() const {
                return value_ ? static_cast<float>(atof(value_)) : 0.0f;
            }
            inline  bool GetBool() const {
                return value_ ? atoi(value_) > 0 : false;
            }
            inline double GetDouble() const {
                return value_ ? static_cast<double>(atof(value_)) : 0.0f;
            }
            inline ::std::int8_t GetInt8() const {
                return value_ ? static_cast<::std::int8_t>(atol(value_)) : ::std::int8_t(0);
            }
            inline ::std::int32_t GetInt32() const {
                return value_ ? static_cast<::std::int32_t>(atol(value_)) : ::std::int32_t(0);
            }
            inline ::std::uint8_t GetUInt8() const {
                return value_ ? static_cast<::std::uint8_t>(atol(value_)) : ::std::uint8_t(0);
            }
            inline ::std::uint16_t GetUInt16() const {
                return value_ ? static_cast<::std::uint16_t>(atol(value_)) : ::std::uint16_t(0);
            }
            inline ::std::int16_t GetInt16() const {
                return value_ ? static_cast<::std::int16_t>(atol(value_)) : ::std::int16_t(0);
            }
            inline ::std::uint32_t GetUInt32() const {
                return value_ ? static_cast<::std::uint32_t>(atol(value_)) : ::std::uint32_t(0);
            }

            ::std::uint64_t GetUInt64() const;
            ::std::int64_t GetInt64() const;

        private:
            const char     *value_;
            enum DataTypes  type_;

            DISALLOW_COPY_AND_ASSIGN(Field);
        };

    }
}

#endif // NODE_DB_RDB_FIELD_H_
